package com.dy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.pojo.dto.DyBlogByConDTO;
import com.dy.pojo.dto.DyBlogDTO;
import com.dy.pojo.entity.DyBlogEntity;
import com.dy.pojo.vo.DyBlogTagTypeVo;
import com.dy.pojo.vo.DyBlogVO;
import com.dy.service.DyBlogService;
import com.dy.utils.ApiResousUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)博客详情控制层
 * @Date: 2021/6/8 14:54
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/blog")
public class DyBlogController{
    @Autowired
    private DyBlogService dyBlogService;

    /**
     * 查询全部博客信息
     * @param page
     * @param pageSize
     * @return ApiResousUtil
     */
    @RequiresRoles("admin")
    @GetMapping("/queryBlog")
    public Page<DyBlogEntity> queryBlog(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                       @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        try{
            Page<DyBlogEntity> selectPage = dyBlogService.queryBolg(page, pageSize);
            return selectPage;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 删除
     * @param id
     * @return ApiResousUtil
     */
    @GetMapping("/deleteBlog")
    public ApiResousUtil<String> deleteBlog(@RequestParam("id")Long id){
        try {
            List<DyBlogEntity> list = dyBlogService.list(new QueryWrapper<DyBlogEntity>().eq("id", id));
            if (id == null || list.size() == 0) {
                return ApiResousUtil.error("删除失败！id为空不存在");
            }
            dyBlogService.deleteBlog(id);
            return ApiResousUtil.ok("删除成功！");
        }catch (Exception e){
            return ApiResousUtil.error("删除失败！未知错误");
        }
    }

    /**
     * 博客回收站,显示所有已经删除的博客
     * @return ApiResousUtil
     */
    @GetMapping("/recycle")
    public ApiResousUtil<List<DyBlogEntity>> recycleBlog(){
        List<DyBlogEntity> recylist = dyBlogService.recycleBlog();
        return ApiResousUtil.ok(recylist);
    }

    /**
     *回收站定时删除（每个月15日上午十点15触发）
     * @return ApiResousUtil
     */
    @Scheduled(cron = "0 15 10 15 * ?")
    @RequestMapping("/taskDeleteBlog")
    public ApiResousUtil<String> taskDeleteBlog(){
        dyBlogService.taskDeleteBlog();
        return ApiResousUtil.ok("回收站删除成功！");
    }

    /**
     * 博客删除状态更改
     * @param id id
     * @return ApiResousUtil
     */
    @RequestMapping("/updateDeleted")
    public ApiResousUtil<String> updateDeleted(@RequestParam("id")Long id){
        if (id == null) {
            return ApiResousUtil.error("删除失败！没有id");
        }
        dyBlogService.updateDeleted(id);
        return ApiResousUtil.ok("修改成功");
    }

    /**
     * 博客修改
     * @param dyBlogDTO
     * @return ApiResousUtil
     */
    @PostMapping("/updateBlog")
    public ApiResousUtil<String> updateBlog(@RequestBody DyBlogDTO dyBlogDTO){
        //System.out.println("dyBlogDTO = " + dyBlogDTO.toString());
        try{
            dyBlogService.updateBlog(dyBlogDTO);
            return ApiResousUtil.ok();
        }catch (Exception e){
            System.out.println("e = " + e);
            return ApiResousUtil.error("未知错误！");
        }
    }

    /**
     * 博客添加
     * @param dyBlogDTO
     * @return ApiResousUtil
     */
    @PostMapping("/create")
    public ApiResousUtil<String> createBlog(@RequestBody DyBlogDTO dyBlogDTO, HttpSession session){
        String code = (String) session.getAttribute("code");
        dyBlogService.create(dyBlogDTO,code);
        return ApiResousUtil.ok();
    }
    /**
     * 根据id查询数据
     * @param id
     * @return
     **/
    @PostMapping("/selectBlogById")
    public ApiResousUtil<DyBlogTagTypeVo> selectBlogById(@RequestParam("id")Long id){
        DyBlogTagTypeVo dyBlogTagTypeVo = dyBlogService.selectBlogById(id);
        return ApiResousUtil.ok(dyBlogTagTypeVo);
    }
    /**
     * 多条件查询
     * @param dyBlogByConDTO
     * @return
     **/
    @PostMapping("/selectBlogByCon")
    public ApiResousUtil<Object> selectBlogByCon(@RequestBody DyBlogByConDTO dyBlogByConDTO){
        if (dyBlogByConDTO == null) {
            return ApiResousUtil.error("数据为空");
        }
        List<DyBlogVO> list = dyBlogService.selectBlogByCon(dyBlogByConDTO);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setRecommend(list.get(i).getRecommend() == "1" ? "是":"否");
            list.get(i).setPublished(list.get(i).getPublished() == "1" ? "发布":"保存");
            list.get(i).setTypeName(list.get(i).getName());
        }
        return ApiResousUtil.ok(list);
    }
}
